package com.yulong.web.controller;




import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yulong.model.pojo.UmsAdmin;
import com.yulong.web.dto.UmsAdminLoginParam;
import com.yulong.web.dto.UmsUpdateRoleParam;
import com.yulong.web.server.Impl.UmsAdminServerImpl;
import com.yulong.web.utils.RedisUtil;
import com.yulong.web.utils.ResponseMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("admin")
@Api("后台用户管理")
public class UmsAdminController {


    @Autowired
    private UmsAdminServerImpl userInfoServerImpl;

    @Autowired
    private RedisUtil redisUtil ;


    @GetMapping("list")
    @ApiOperation("分页查询所有用户")
    public ResponseMap selectAll(@RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize,
                                  @RequestParam(value = "keyword",required=false) String username){

                    PageHelper.startPage(pageNum, pageSize);
                     PageInfo<UmsAdmin> umsAdminPageInfo = new PageInfo<>(userInfoServerImpl.selectList(username));
                     return ResponseMap.ok(umsAdminPageInfo);
    }




    @PostMapping("register")
    @ApiOperation("用户注册")
    public ResponseMap insertUserInfo(@RequestBody UmsAdmin umsAdmin){
        return userInfoServerImpl.register(umsAdmin);
    }


    @PostMapping("login")
    @ApiOperation("登录")
    public ResponseMap login(@RequestBody UmsAdminLoginParam umsAdminLoginParam){

        ResponseMap login = userInfoServerImpl.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        return login;
    }


    @PostMapping("deleteById/{id}")
    @ApiOperation("删除用户")
    public ResponseMap deleteById(@PathVariable String id){
        ResponseMap responseMap = userInfoServerImpl.deleteById(id);
        return responseMap;
    }



    @PostMapping("updateAdmin")
    @ApiOperation("更新用户")
    public ResponseMap login(@RequestBody UmsAdmin umsAdmin){
        ResponseMap responseMap = userInfoServerImpl.updateById(umsAdmin);
        return responseMap;
    }

    @PostMapping("role/update")
    @ApiOperation("为角色分配用户")
    public ResponseMap updateRole(@RequestBody UmsUpdateRoleParam param){
        ResponseMap responseMap = userInfoServerImpl.updateRole(param.getAdminId(),param.getRoleIds());
        return responseMap;
    }


    @GetMapping("/role/{adminId}")
    @ApiOperation("查询当前用户的角色")
    public ResponseMap queryAdminRoleRelationList(@PathVariable Long adminId){
        return userInfoServerImpl.queryAdminRoleRelationList(adminId);
    }

    @PostMapping("updateStatus/{id}")
    @ApiOperation("停用启用用户")
    public ResponseMap updateStatus(@PathVariable(value = "id") Long id,@RequestParam(value = "status") Long status){
        ResponseMap responseMap = userInfoServerImpl.updateByStatus(id,status);
        return responseMap;
    }

}
